package com.aj.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.aj.application.LoveHomeApplication;

public class MobileUtil {
	private static final String OS_NAME = "android";
	private static final String TAG = MobileUtil.class.getName();

	public static String getMacAddress(Context mContext) {
		String result = "";
		WifiManager wifiManager = (WifiManager) mContext
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();
		result = wifiInfo.getMacAddress();
		return result;
	}

	public static String getImei(Context mContext) {
		TelephonyManager mTm = (TelephonyManager) mContext
				.getSystemService(Context.TELEPHONY_SERVICE);
		String imei = mTm.getDeviceId();
		return imei;
	}

	public static String getOs() {
		String os = OS_NAME + " " + android.os.Build.VERSION.RELEASE;
		return os;
	}

	public static String getMobile() {
		String mobile = android.os.Build.MODEL;
		return mobile;
	}

	public static int getVersionCode(Context mContext) {
		int versionCode = Integer.MAX_VALUE;
		try {
			if (mContext != null) {
				PackageManager pm = mContext.getPackageManager();
				PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(),
						PackageManager.GET_CONFIGURATIONS);
				versionCode = pi.versionCode;
			}
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return versionCode;
	}

	public static String getVersionName(Context mContext) {
		String versionName = "";
		try {
			PackageManager pm = mContext.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(),
					PackageManager.GET_CONFIGURATIONS);
			versionName = pi.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return versionName;
	}

	// 是否为中国移动手机号码
	public static boolean isCMCCPhoneNumber(String phone) {
		String regex = "^((13[4-9])|(147)|(15[0-2,7-9])|(18[2-4,7-8]))\\d{8}$";
		Pattern p = Pattern.compile(regex);
		Matcher matcher = p.matcher(phone);
		if (matcher.find()) {
			return true;
		}
		return false;
	}

	public static boolean isWifiConnect(Context context) {
		ConnectivityManager connectMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connectMgr.getActiveNetworkInfo();
		if (info != null && info.getType() == ConnectivityManager.TYPE_WIFI) {
			return true;
		} else {
			return false;
		}
	}

	public static String getSdcardPath() {
		String file_dir = "";
		// SD卡是否存在
		boolean isSDCardExist = Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState());
		// Environment.getExternalStorageDirectory()相当于File file=new
		// File("/sdcard")
		boolean isRootDirExist = Environment.getExternalStorageDirectory()
				.exists();
		if (isSDCardExist && isRootDirExist) {
			file_dir = Environment.getExternalStorageDirectory() + "";
		} else {
			// MyApplication.getInstance().getFilesDir()返回的路劲为/data/data/PACKAGE_NAME/files，其中的包就是我们建立的主Activity所在的包
			file_dir = "/";
		}
		return file_dir;
	}

	public static String getFilePath() {
		String file_dir = "";
		// SD卡是否存在
		boolean isSDCardExist = Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState());
		// Environment.getExternalStorageDirectory()相当于File file=new
		// File("/sdcard")
		boolean isRootDirExist = Environment.getExternalStorageDirectory()
				.exists();
		if (isSDCardExist && isRootDirExist) {
			file_dir = Contants.APP_PATH;
		} else {
			// MyApplication.getInstance().getFilesDir()返回的路劲为/data/data/PACKAGE_NAME/files，其中的包就是我们建立的主Activity所在的包
			file_dir = LoveHomeApplication.application.getFilesDir()
					.getAbsolutePath();
		}
		return file_dir;
	}

	public static File saveBitmapToSDCard(String bitName, Bitmap mBitmap) {
		File f = new File(getFilePath() + "/"
				+ Contants.HEADER_IMAGE_PATH_STRING);
		if (!f.exists()) {
			f.mkdirs();
		}
		File imageFile = new File(f.getAbsoluteFile() + "/" + bitName);
		try {
			imageFile.createNewFile();
		} catch (IOException e) {
			Log.e(TAG, "在保存图片时出错：" + e.toString());
		}
		FileOutputStream fOut = null;
		try {
			fOut = new FileOutputStream(imageFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
		try {
			fOut.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			fOut.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return imageFile;
	}
}
